package cn.tom.controller;

import cn.tom.dao.LargeFileDao;
import cn.tom.dao.UserDao;
import cn.tom.entity.Largefile;
import cn.tom.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;

@Controller
@RequestMapping("/user")
public class LoginController {
     @Autowired
     UserDao userDao;
    @Autowired
    LargeFileDao largeFileDao;

    //    去登录，获取登录的页面
    @GetMapping("/go2login")
    public String go2login(){
        return "/login";
    }
    @PostMapping("/login")
    public String login(String phone, String pwd, Model model, HttpServletRequest request) {
//        如果登录页面
        System.out.println("phone=" + phone);
        System.out.println("pwd=" + pwd);
        HttpSession session = request.getSession();
        System.out.println("session Id=" + session.getId());
        User user = userDao.findOneByPhone(phone);

        if(user==null){
            System.out.println("数据库里面该没有手机账户");
            model.addAttribute("loginmsg","登录失败");
            return "/login";
        }
        if (pwd!=null && pwd.equals(user.getPwd())){
            System.out.println("登陆成功");
            session.setAttribute("who",user);
            if (user.getRole().equals("stu")) {
                model.addAttribute("role","stu");
            }
            if (user.getRole().equals("tea")) {
                model.addAttribute("role","tea");
            }
            if (user.getRole().equals("adm"))
            {
                model.addAttribute("role","adm");
            }
            System.out.println(user);
            /*model.addAttribute("pic",user.getPic());*/
            model.addAttribute("loginmsg",
                    "登陆成功");
            return "login";
        }else {
            System.out.println("登录失败,密码不对");
            model.addAttribute("loginmsg",
                    "登录失败-密码错误");
            return "login";
        }
    }

    /*退出*/
    @GetMapping("/logout")
    public String logout(HttpSession session, Model model){
        System.out.println("销毁对象");
        session.invalidate();
        model.addAttribute("loginmsg","退出，请重新登录");
        return "/login";
    }

    @GetMapping("/showimg/{pic}")
    public void  showimg(@PathVariable("pic")String id,
                         HttpServletResponse response) throws IOException {
        Largefile one =largeFileDao.findOne(id);
        Object conent=one.getContent();

        OutputStream os=response.getOutputStream();
        response.setContentType("image/jpg");
//
        byte[] buf= (byte[])conent;
        os.write(buf);
        os.flush();

    }
}
